<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2021-05-17 15:15:38
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2021-05-26 18:00:20
 */


namespace admin\controllers\auth;

use admin\controllers\AController;
use Yii;
use admin\models\auth\AuthRoute;
use admin\models\searchs\auth\AuthRoute as AuthRouteSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\controllers\BaseController;
use common\helpers\ArrayHelper;
use common\helpers\ErrorsHelper;
use common\helpers\ResultHelper;
use diandi\addons\models\DdAddons;
use diandi\admin\models\Route;

/**
 * RouteController implements the CRUD actions for AuthRoute model.
 */
class RouteController extends AController
{
    public $modelClass = '';

    public $modelSearchName = "AuthRouteSearch";



    /**
     * Lists all AuthRoute models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new AuthRouteSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        
        return ResultHelper::json(200, '获取成功', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionAvailable()
    {
        $model = new Route();
        $list = $model->getRoutes();

        $available = $list['available'];

        return ResultHelper::json(200, '获取成功', $available);
    }

    /**
     * Displays a single AuthRoute model.
     * @param integer $id
     * @param string $name
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        $view = $this->findModel($id);
        return ResultHelper::json(200, '获取成功', $view);
    }

    /**
     * Creates a new AuthRoute model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        global $_GPC;

        $model = new AuthRoute();

        if (Yii::$app->request->isPost) {
            $data = Yii::$app->request->post();                
            $data['type'] = $_GPC['module_name'] == 'sys' || empty($_GPC['module_name']) ?0:1;
            if ($model->load($data,'') && $model->save()) {
                return ResultHelper::json(200, '创建成功', $model);
            } else {
                $msg = ErrorsHelper::getModelError($model);
                return ResultHelper::json(400, $msg);
            }
        }
    }

    /**
     * Updates an existing AuthRoute model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param string $name
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if (Yii::$app->request->isPut) {
            if ($model->load(Yii::$app->request->post(),'') && $model->save()) {
                return ResultHelper::json(200, '编辑成功', $model);
            } else {
                $msg = ErrorsHelper::getModelError($model);
                return ResultHelper::json(400, $msg);
            }
        }
    }

    /**
     * Deletes an existing AuthRoute model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param string $name
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return ResultHelper::json(200, '删除成功');

    }

    /**
     * Finds the AuthRoute model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @param string $name
     * @return AuthRoute the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AuthRoute::findOne(['id' => $id])) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
